{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "url = \"https://github.com/python-windrose/windrose/raw/main/samples/sample_wind_poitiers.csv\"\n",
    "df = pd.read_csv(url, parse_dates=[\"Timestamp\"])\n",
    "df = df.set_index(\"Timestamp\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "df[\"speed_x\"] = df[\"speed\"] * np.sin(df[\"direction\"] * np.pi / 180.0)\n",
    "df[\"speed_y\"] = df[\"speed\"] * np.cos(df[\"direction\"] * np.pi / 180.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(8, 8), dpi=80)\n",
    "x0, x1 = ax.get_xlim()\n",
    "y0, y1 = ax.get_ylim()\n",
    "ax.set_aspect(\"equal\")\n",
    "df.plot(kind=\"scatter\", x=\"speed_x\", y=\"speed_y\", alpha=0.05, ax=ax)\n",
    "Vw = 80\n",
    "ax.set_xlim([-Vw, Vw])\n",
    "ax.set_ylim([-Vw, Vw]);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.cm as cm\n",
    "\n",
    "from windrose import WindroseAxes\n",
    "\n",
    "ax = WindroseAxes.from_ax()\n",
    "ax.bar(\n",
    "    df.direction.values, df.speed.values, bins=np.arange(0.01, 8, 1), cmap=cm.hot, lw=3\n",
    ")\n",
    "ax.set_legend();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from windrose import plot_windrose\n",
    "\n",
    "plot_windrose(df, kind=\"contour\", bins=np.arange(0.01, 8, 1), cmap=cm.hot, lw=3);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bins = np.arange(0, 30 + 1, 1)\n",
    "bins = bins[1:]\n",
    "bins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_windrose(df, kind=\"pdf\", bins=np.arange(0.01, 30, 1));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.histogram(df[\"speed\"], bins=bins)[0]\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Wind rose for a given month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_month(df, t_year_month, *args, **kwargs):\n",
    "    by = \"year_month\"\n",
    "    df[by] = df.index.map(lambda dt: (dt.year, dt.month))\n",
    "    df_month = df[df[by] == t_year_month]\n",
    "    ax = plot_windrose(df_month, *args, **kwargs)\n",
    "    return ax\n",
    "\n",
    "\n",
    "plot_month(df, (2014, 7), kind=\"contour\", bins=np.arange(0, 10, 1), cmap=cm.hot);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_month(df, (2014, 8), kind=\"contour\", bins=np.arange(0, 10, 1), cmap=cm.hot);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_month(df, (2014, 9), kind=\"contour\", bins=np.arange(0, 10, 1), cmap=cm.hot);"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
